class Result < ActiveRecord::Base
  belongs_to :work
  belongs_to :scan
    #评论倍值
    E_COMMENT = 50

  def get_ranking_point
    clicks_point = clicks.to_f * click_revised(true)
    comment_point = comments.to_f * comment_revised
    (clicks_point + comment_point + danmu.to_f) * space_fix
    format("%.2f", (clicks_point + comment_point + danmu.to_f) * space_fix).to_f
  end

  #娱乐版&youku|tudou修正值
  def space_fix
     if (work.ac_type.name == '娱乐' || work.ac_type.name == '新番') && !work.youku_and_tudou.empty?
       0.5
     else
       1
     end
  end
  
  #点击Part修正
  def self.get_click_revised parts, simple = false
    if simple
=begin
      case parts
         when 1 then 1.0
         when 2 then 0.7
         when 3 then 0.5
         when 4 then 0.4
         when 5..10 then 0.3
        when 11..30 then 0.2
        when 31..105 then 0.1
         else
           0
      end
=end
      format("%.2f", 2.0 / (parts + 1)).to_f
    else
      1 * 1.0 / parts
    end
  end

  def click_revised simple = false
    Result.get_click_revised work.part_count, simple
  end
  
  #评论修正
  def self.get_comment_revised clicks, comments
    if clicks.to_i >= comments.to_i * E_COMMENT
      E_COMMENT
    else
      E_COMMENT * clicks.to_f * 2 / (clicks.to_f + comments.to_f * E_COMMENT)
    end
  end

  def comment_revised
    Result.get_comment_revised clicks, comments
  end
  
  #时间修正
  def self.get_date_revised date, std_date
    begin
      if std_date && date.to_datetime.compare_with_coercion(std_date + ' 00:00:00') == -1
        E_DATE
      else
        1.0
      end
    rescue
      1.0
    end
  end
  
  def self.point_str point
    str = ""
    point_s = point.to_s
    fushu = point_s.first == '-' ? true : false
    size = point_s.size - ( fushu ? 1 : 0 )
    if size <= 3
      point_s
    else
      point_s.delete('-').split('').each_with_index do |ch, i|
        str << "," if i > 0 && (size - i) % 3 == 0
        str << ch
      end
      str = '-' + str if fushu
      str
    end
  end

  #修复弹幕数据用
  def fix_danmu
    unless self.work.author_id
      self.work.get_info false
    end
    print "====#{work.wid} #{danmu} => "
    i = 0
    while i += 1
      page = Hpricot.XML(open("http://#{BILI_PATH}/plus/search.php?keyword=#{URI.encode top20name}&typeid=#{self.work.ac_type.page_code}&amid=#{self.work.author.mid}&PageNo=#{i}"))
      items = page.search("div[@class=item]")
      if items.empty?
        print "not found\n"
        break
      end
      flag = false
      page.search("div[@class=item]").each do |item|
        id_uri = item.search("h3").first.search("a").first.get_attribute("href")
        if %[http://#{BILI_PATH}/video/av#{self.work.wid}/] == id_uri || %[http://#{BILI_PATH}/plus/view.php?aid=#{self.work.wid}] == id_uri
          if item.search("span")[3].inner_text == " "
            self.update_attribute :danmu, work.flash_vars.inject(0){|re, item| re += item.get_danmu_count}
            p self.danmu
          else
            print %!#{item.search("span")[3].inner_text}\n!
            self.update_attribute :danmu, item.search("span")[3].inner_text.to_i
          end
          flag = true
          break
        end
      end
      break if flag
    end
  end

  def top20name n=19
    Iconv.iconv('GB18030//IGNORE', 'UTF-8//IGNORE', self.work.name[0..n]).first
  rescue
    top20name n-1
  end
end
